package csc.fresher.finalgroupfour.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.fresher.finalgroupfour.domain.Logging;
import csc.fresher.finalgroupfour.domain.User;

@Repository("loggingDAO")
public class LoggingDAO {
	@PersistenceContext
	private EntityManager em; // Entity Manager is acquired by container.

	/**
	 * Add a new customer.
	 * 
	 * @param customer
	 */
	@Transactional
	public void addLogging(Logging log) {
		em.persist(log);
	}

	@Transactional
	public List<Logging> listLoggings() {
		List<Logging> logs = new ArrayList<Logging>();
		TypedQuery<Logging> query = em.createQuery(
				"SELECT log FROM Logging log", Logging.class);

		logs = query.getResultList();
		if (logs.size() > 0) {
			return logs;
		}
		return null;
	}

	@Transactional
	public List<Logging> listLoggings(User user) {
		List<Logging> logs = new ArrayList<Logging>();
		TypedQuery<Logging> query = em.createQuery(
				"SELECT log FROM Logging log WHERE log.user = :user",
				Logging.class)
				.setParameter("user", user);

		logs = query.getResultList();
		if (logs.size() > 0) {
			return logs;
		}
		return null;
	}

	/**
	 * Update customer informations.
	 * 
	 * @param customer
	 */
	@Transactional
	public void updateLogging(Logging log) {
		em.merge(log);
	}

	/**
	 * Remove a customer from database.
	 * 
	 * @param customer
	 */
	@Transactional
	public void deleteLogging(Logging log) {
		em.remove(log);
	}
}
